package com.dms.modules.merchant.service.impl;

import com.dms.modules.merchant.entity.Merchant;
import com.dms.modules.merchant.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Slf4j
@Service("merchantUserDetailsService")
public class MerchantUserDetailsService implements UserDetailsService {

    private final MerchantService merchantService;

    public MerchantUserDetailsService(MerchantService merchantService) {
        this.merchantService = merchantService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Merchant merchant = merchantService.getByUsername(username);
        if (merchant == null) {
            log.warn("商家不存在: {}", username);
            throw new UsernameNotFoundException("商家不存在");
        }
        log.debug("商家认证成功: {}", username);
        return merchant;
    }
} 